﻿using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.NetworkAnalysis;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Color = System.Drawing.Color;

namespace classTest
{
    /// <summary>
    /// ViewFlowersRouteWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ViewFlowersRouteWindow : Window
    {
        private readonly Dictionary<string, MapPoint> routeOne = new Dictionary<string, MapPoint>()
        {
            {"正门口(地铁站)",new MapPoint(12617655.684286,2650838.85873907,new SpatialReference(3857))},
            {"湿地公园",new MapPoint(12617962.1623282,2651040.60332828,new SpatialReference(3857))},
            {"三角市",new MapPoint(12617969.9026881,2651292.04374752,new SpatialReference(3857))},
            {"旧保安处",new MapPoint(12617911.0383811,2651582.22487653,new SpatialReference(3857))},
            {"嵩山区",new MapPoint(12618527.5578926,2651629.009858,new SpatialReference(3857))},
            {"农学院",new MapPoint(12618468.3109567,2651510.21208291,new SpatialReference(3857))},
            {"资环学院",new MapPoint(12618319.4879986,2651296.49334433,new SpatialReference(3857))},
            {"校医院东门",new MapPoint(12618248.9682183,2651204.2751701,new SpatialReference(3857))},
            {"茶山广场",new MapPoint(12618497.3272032,2650421.36711358,new SpatialReference(3857))}
        };
        private readonly Dictionary<string, MapPoint> routeTwo = new Dictionary<string, MapPoint>()
        {
            {"西园",new MapPoint(12617166.3915043,2651818.86665153,new SpatialReference(3857))},
            {"工程学院",new MapPoint(12617012.0643088,2651608.51571032,new SpatialReference(3857))},
            {"新学活",new MapPoint(12617303.4898376,2651564.50178605,new SpatialReference(3857))},
            {"第三教学楼",new MapPoint(12617676.2744387,2651517.82035122,new SpatialReference(3857))},
            {"艺术学院",new MapPoint(12617743.6290803,2651600.51317864,new SpatialReference(3857))},
            {"三角市",new MapPoint(12617948.4836109,2651412.34606845,new SpatialReference(3857))},
            {"湿地公园",new MapPoint(12618069.8553029,2651039.91124924,new SpatialReference(3857))},
            {"经管学院",new MapPoint(12618385.9950376,2650851.66635677,new SpatialReference(3857))},
            {"第四教学楼",new MapPoint(12619285.2932012,2650820.64965121,new SpatialReference(3857))},
            {"五山公寓",new MapPoint(12619533.5459365,2650672.14219439,new SpatialReference(3857))},
            {"公寓东",new MapPoint(12619648.5679713,2650841.48019009,new SpatialReference(3857))},
            {"荷园",new MapPoint(12619490.6321222,2651750.28648144,new SpatialReference(3857))},
        };
        private readonly Dictionary<string, MapPoint> routeThree = new Dictionary<string, MapPoint>()
        {
            {"西园",new MapPoint(12617166.3915043,2651818.86665153,new SpatialReference(3857))},
            {"工程学院",new MapPoint(12617012.0643088,2651608.51571032,new SpatialReference(3857))},
            {"新学活",new MapPoint(12617303.4898376,2651564.50178605,new SpatialReference(3857))},
            {"第三教学楼",new MapPoint(12617676.2744387,2651517.82035122,new SpatialReference(3857))},
            {"艺术学院",new MapPoint(12617743.6290803,2651600.51317864,new SpatialReference(3857))},
            {"生命与科学学院",new MapPoint(12618643.5974444,2651635.00168004,new SpatialReference(3857))},
            {"荷园",new MapPoint(12619490.6321222,2651750.28648144,new SpatialReference(3857))}
        };
        private MapView MyMapView;
        private Graphic routeGraphic;
        private string networkPath;
        private string networkName;
        private GraphicsOverlay _viewGraphicsOverlay = new GraphicsOverlay();


        public ViewFlowersRouteWindow(MapView mapView,string networkPath1, string networkName1)
        {
            InitializeComponent();
            MyMapView = mapView;
            networkPath = networkPath1;
            networkName = networkName1;

            MyMapView.GraphicsOverlays.Add(_viewGraphicsOverlay);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBoxItem cbi1 = new ComboBoxItem();
            cbi1.Content = "路线一";
            cbi1.DataContext = routeOne;
            routeComboBox.Items.Add(cbi1);

            ComboBoxItem cbi2 = new ComboBoxItem();
            cbi2.Content = "路线二";
            cbi2.DataContext = routeTwo;
            routeComboBox.Items.Add(cbi2);

            ComboBoxItem cbi3 = new ComboBoxItem();
            cbi3.Content = "路线三";
            cbi3.DataContext = routeThree;
            routeComboBox.Items.Add(cbi3);

            routeComboBox.SelectedIndex = 0;
        }

        private void RouteComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (routeComboBox.SelectedIndex == -1) return;
            pointListBox.Items.Clear();
            Dictionary<string, MapPoint> points = (routeComboBox.SelectedItem as ComboBoxItem).DataContext as Dictionary<string, MapPoint>;
            for (int i = 0; i < points.Count; i++)
            {
                ListBoxItem lbi = new ListBoxItem();
                var item = points.ElementAt(i);
                lbi.Content = item.Key;
                lbi.DataContext = item.Value;
                pointListBox.Items.Add(lbi);
            }
            pointListBox.SelectedIndex = 0;
            LoadRoute(points);
        }

        // 加载路线
        private async void LoadRoute(Dictionary<string, MapPoint> points)
        {
            _viewGraphicsOverlay.Graphics.Clear();
            Console.WriteLine(networkPath);
            List<Stop> routeStops = new List<Stop>();
            try
            {
                RouteTask solveRouteTask = await RouteTask.CreateAsync(networkPath,networkName);
                RouteParameters routeParams = await solveRouteTask.CreateDefaultParametersAsync();
                routeParams.ReturnStops = true;
                routeParams.ReturnDirections = true;
                foreach(MapPoint p in points.Values)
                {
                    var picPath = @"..\..\Image\Icon\via.png";
                    
                    PictureMarkerSymbol pSymbol;
                    using (System.IO.FileStream picStream = new System.IO.FileStream(picPath,
                    System.IO.FileMode.Open))
                    {
                        pSymbol = await PictureMarkerSymbol.CreateAsync(picStream);
                    }
                    pSymbol.Width = 30;
                    pSymbol.Height = 30;
                    pSymbol.OffsetY = pSymbol.Height / 2;
                    Graphic pGraphic = new Graphic(p, pSymbol);
                    _viewGraphicsOverlay.Graphics.Add(pGraphic);

                    routeStops.Add(new Stop(p));
                }
                routeParams.SetStops(routeStops);
                RouteResult solveRouteResult = await solveRouteTask.SolveRouteAsync(routeParams);
                Route route = solveRouteResult.Routes.First();
                Polyline routePolyline = route.RouteGeometry;
                SimpleLineSymbol routeSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Blue, 3.0);
                routeGraphic = new Graphic(routePolyline, routeSymbol);
                _viewGraphicsOverlay.Graphics.Add(routeGraphic);
                await MyMapView.SetViewpointGeometryAsync(routeGraphic.Geometry.Extent, 50);
            }
            catch
            {
                MessageBox.Show("出现错误！(请重新设置起始点)");
            }
        }
        // 缩放至全路线
        private async void ZoomRouteButton_Click(object sender, RoutedEventArgs e)
        {
            if (routeGraphic != null)
            {
                await MyMapView.SetViewpointGeometryAsync(routeGraphic.Geometry.Extent,50);
            }
        }
        // 缩放至某点
        private async void PointListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (pointListBox.SelectedIndex == -1) return;
            MapPoint mp = (pointListBox.SelectedItem as ListBoxItem).DataContext as MapPoint;
            if (routeComboBox.SelectedIndex == 0)
            {
                pointDescLabel.Content = routeOne.Keys.ElementAt(pointListBox.SelectedIndex);
            }
            else if(routeComboBox.SelectedIndex == 1)
            {
                pointDescLabel.Content = routeTwo.Keys.ElementAt(pointListBox.SelectedIndex);
            }
            else if (routeComboBox.SelectedIndex == 2)
            {
                pointDescLabel.Content = routeThree.Keys.ElementAt(pointListBox.SelectedIndex);
            }

            Random random = new Random();
            var picPath = $"..\\..\\Image\\scauImage\\{random.Next(1, 39)}.jpg";
            pointImage.Source= new BitmapImage(new Uri(picPath,UriKind.Relative));

            await MyMapView.SetViewpointCenterAsync(mp, 1517);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            MyMapView.GraphicsOverlays.Remove(_viewGraphicsOverlay);
        }
    }
}
